#!/bin/sh
# Copyright (C) 2000-2016 Synology Inc. All rights reserved.


insert_module()
{
	local module=$1
	local symbol=$2
	if ! /sbin/lsmod | grep -q "\<${symbol}\>"; then
		if [ "xkvm_intel" == "x${symbol}" ]; then
			/sbin/insmod /lib/modules/kvm-intel.ko nested=1 enable_apicv=0 || \
				( /usr/bin/logger -p err "Failed to insert ${module}, stop"; false )
		else
			/sbin/insmod /lib/modules/${module} || \
				( /usr/bin/logger -p err "Failed to insert ${module}, stop"; false )
		fi
	else
		true
	fi
}

insert_kernel_modules()
{
	insert_module kvm.ko kvm
    it=`grep vmx /proc/cpuinfo`
    amd=`grep svm /proc/cpuinfo`
    if [ ! -z "${it}" ]; then
		insert_module kvm-intel.ko kvm_intel
    elif [ ! -z "${amd}" ]; then
		insert_module kvm-amd.ko kvm_amd
    else
        echo 0
    fi

	insert_module vhost.ko vhost
	insert_module tun.ko tun
	insert_module vhost_net.ko vhost_net
	# vhost_scsi.ko were inserted in synoiscsiep
	insert_module pci-stub.ko pci_stub

	/usr/syno/bin/synoiscsiep --insmod vhost
	/usr/syno/bin/synoiscsiep --insmod loopback
	/usr/syno/bin/synoiscsiep --startall loopback
	/usr/syno/bin/synoiscsiep --startall vhost-scsi
}
remove_kernel_modules()
{
	/usr/syno/bin/synoiscsiep --stopall loopback
	/usr/syno/bin/synoiscsiep --rmmod loopback
	/usr/syno/bin/synoiscsiep --stopall vhost-scsi
	/usr/syno/bin/synoiscsiep --rmmod vhost

	rmmod pci_stub
	rmmod vhost_net
	rmmod vhost
	rmmod tun
	it=`grep vmx /proc/cpuinfo`
	amd=`grep svm /proc/cpuinfo`
	if [ ! -z "${it}" ]; then
		rmmod kvm_intel
	elif [ ! -z "${amd}" ]; then
		rmmod kvm_amd
	else
		echo 0
	fi
	rmmod kvm
}

case $1 in
	start)
		ret=`insert_kernel_modules`
		if [ "$ret" != "0" ]; then

            export PATH=$PATH:/usr/local/synokvm/bin:/usr/local/synokvm/sbin
			export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/synokvm/lib
			/usr/local/synokvm/sbin/virtlogd -d
			/usr/local/synokvm/sbin/libvirtd -l -d -f /usr/local/synokvm/etc/libvirt/libvirtd.conf

            export PYTHONPATH=$PYTHONPATH:/usr/local/synokvm/python2.7/site-packages
            iptables -N REJECT
            python2 /usr/local/synokvm/webvirtmgr/manage.py run_gunicorn -c /usr/local/synokvm/webvirtmgr/conf/gunicorn.conf.py &
            python2 /usr/local/synokvm/webvirtmgr/console/webvirtmgr-console &

            for f in `ls /root/.synokvm/xmls/*.xml`; do 
                /usr/local/synokvm/bin/virsh define ${f}
                rm -f ${f}
            done
			exit 0
		else
			exit 1
		fi
		;;
	stop)
        export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/synokvm/lib
        for f in `/usr/local/synokvm/bin/virsh list --state-running --name 2>/dev/null`;do
            virsh shutdown ${f}
        done
        sleep 5
        iptables -X REJECT
		killall libvirtd
		killall virtlogd
        ps -ef|grep webvirtmgr-console|grep -v grep|awk '{print "kill -9 "$2}'|sh
        ps -ef|grep run_gunicorn|grep -v grep|awk '{print "kill -9 "$2}'|sh
		remove_kernel_modules
		exit 0
		;;
	status)
		if [ -f "/usr/local/synokvm/var/run/virtlogd.pid" -a -f "/usr/local/synokvm/var/run/libvirtd.pid" ]; then
			exit 0
		else
			exit 1
		fi
		;;
	killall)
        export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/synokvm/lib
        for f in `/usr/local/synokvm/bin/virsh list --state-running --name 2>/dev/null`;do
            virsh shutdown ${f}
        done
        sleep 5
		killall libvirtd
		killall virtlogd
		remove_kernel_modules
		;;
	log)
		exit 0
		;;
esac
